package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.T;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.*;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.*;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.DateUtil;
import com.zscat.mallplus.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 充值套餐管理Controller
 */
@RestController
@Api(tags = "RechargeController", description = "充值套餐相关管理")
@RequestMapping("/api/dms/recharge")
public class RechargeController {

    @Autowired
    private IRechargeService rechargeService;
    @Autowired
    private IntegralService integralService;
    @Autowired
    private IDmsOrderService orderService;
    @Autowired
    private IQsgStoreService storeService;
    @Autowired
    private IQsgRechargeOrderService rechargeOrderService;

    @IgnoreAuth
    @ApiOperation("根据条件查询所有充值套餐套餐管理列表")
    @GetMapping(value = "/list")
    public Object getDeviceByPage(Long storeId) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByAsc("price");
        qw.eq("store_id", storeId);
        try {
            return new CommonResult().success(rechargeService.list(qw));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }

    @IgnoreAuth
    @ApiOperation("根据条件查询所有充值套餐套餐管理列表")
    @PostMapping(value = "/listH5")
    public Object getDeviceByH5(@RequestBody QsgRecharge recharge) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByAsc("price");
        qw.eq("store_id", recharge.getStoreId());
        try {
            return new CommonResult().success(rechargeService.list(qw));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }

    @IgnoreAuth
    @GetMapping(value = "/getMealById")
    @ApiOperation(value = "根据id查询套餐")
    public Object getMealById(Long mealId) {
        try {
            return new CommonResult().success(rechargeService.getById(mealId));
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @ApiOperation("保存设备套餐表")
    @PostMapping(value = "/create")
    public Object saveRecharge(@RequestBody QsgRecharge entity) {
        try {
            if (null==entity.getId()){
                entity.setCreateDatetime(new Date());
                rechargeService.save(entity);
            }else {
                rechargeService.updateById(entity);
            }
            return new CommonResult().success(entity);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @ApiOperation("根据条件查询所有充值套餐套餐管理列表")
    @PostMapping(value = "/delete")
    public Object delete(@RequestBody QsgRecharge recharge) {
        try {
            return new CommonResult().success(rechargeService.removeById(recharge.getId()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }

    @IgnoreAuth
    @ApiOperation("根据条件查询所有充值套餐套餐管理列表")
    @GetMapping(value = "/listByMember")
    public Object listByMember(@RequestParam String status) {
        try {
            UmsMember member = UserUtils.getCurrentMember();
            QueryWrapper qw = new QueryWrapper();
            if (status.equals("0")){
                //充值记录
                IPage<QsgRechargeOrder> page = null;
                qw.eq("member_id",member.getId());
                qw.eq("order_status",2);
                qw.orderByDesc("create_datetime");
                page = rechargeOrderService.page(new Page<>(1,9999),qw);
                for (QsgRechargeOrder rechargeOrder : page.getRecords()){
                    QsgStore store = storeService.getById(rechargeOrder.getStoreId());
                    rechargeOrder.setStoreName(store.getName());
                }
                return new CommonResult().success(page);
            }else if (status.equals("1")){
                //查看余额
                IPage<QsgIntegral> page = null;
                qw.eq("member_id",member.getId());
                qw.orderByDesc("create_datetime");
                page = integralService.page(new Page<>(1,9999),qw);
                for (QsgIntegral integral : page.getRecords()){
                    QsgStore store = storeService.getById(integral.getStoreId());
                    integral.setStoreName(store.getName());
                }
                return new CommonResult().success(page);
            }else if (status.equals("2")){
                //消费记录
                IPage<DmsOrder> page = null;
                qw.eq("member_id",member.getId());
                qw.ne("integral",0);
                qw.orderByDesc("create_datetime");
                page = orderService.page(new Page<>(1,9999),qw);
                for (DmsOrder order : page.getRecords()){
                    QsgStore store = storeService.getById(order.getStoreId());
                    order.setStoreName(store.getName());
                }
                return new CommonResult().success(page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }

    @IgnoreAuth
    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据id查询套餐")
    public Object getById(Long id) {
        try {
            return new CommonResult().success(rechargeService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }
}
